<?php
include_once 'Element.php';
/**
 * Formulario Web
 */
class Form {

    private $id;
    private $method;
    private $action;
    private $elements;
    private $labelSubmit;
    private $actionCancel;

    /**
     * Constructor
     * @param String $id Identificador del Formulario Web
     */
    public function __construct($id) {
        $this->id = $id;
        $this->action = '/';
        $this->method = 'POST';
        $this->labelSubmit = 'Enviar';
        $this->actionCancel = 'location.href = "' . $this->action . '"';
    }
    
    /**
     * Establece el Identificador del Formulario
     * @param String $id Identificador del Formulario
     */
    public function setId($id) {
        $this->id = $id;
    }
    
    /**
     * Obtiene el Identificador del Formulario
     * @return String Identidicador del Formulario
     */
    public function getId() {
        return $this->id;
    }
    
    /**
     * Obtiene el METHOD del Formulario (GET|POST)
     * @return String METHOD
     */
    public function getMethod() {
        return $this->method;
    }

    /**
     * Establece el METHOD del Formulario
     * @param String $method GET|POST
     */
    public function setMethod($method) {
        $this->method = $method;
    }

    /**
     * Obtiene el ACTION del Formulario
     * @return String ACTION
     */
    public function getAction() {
        return $this->action;
    }

    /**
     * Establece el ACTION del Formulario
     * @param String $action ACTION
     */
    public function setAction($action) {
        $this->action = $action;
    }

    /**
     * Obtiene la Etiqueta del Botón SUBMIT del Formulario
     * @return String Etiqueta
     */
    public function getLabelSubmit() {
        return $this->labelSubmit;
    }

    /**
     * Establece la Etiqueta del Botón de Formulario
     * @param String $labelSubmit Etiqueta
     */
    public function setLabelSubmit($labelSubmit) {
        $this->labelSubmit = $labelSubmit;
    }

    /**
     * Obtiene la acción que se ejecuta al cancelar el Formulario
     * @return String Url
     */
    public function getActionCancel() {
        return $this->actionCancel;
    }

    /**
     * Establece la acción que se ejecuta al cancelar el Formulario
     * @param String $url Url
     */
    public function setActionCancel($url) {
        $this->actionCancel = 'if(confirm("Se van a perder las modificaciones."))location.href = "' . $url . '"';
    }

    /**
     * Genera el código HTML del formulario Web
     * @return type
     */
    public function generate() {

        $action = $this->action;
        $method = $this->method;

        //funcion que verifica requeridos
        $name = '';
        $id = $this->id;
        $html = array();
        $html[] = "<script type='text/javascript'>";
        $html[] = "function validate() {";
        foreach ($this->elements as $value) {
            if ($value[2]) {
                if (is_array($value[1])) {
                    foreach ($value[1] as $elemento) {
                        $nombre = $elemento->getId();
                        $html[] = "if(document.getElementById('$id').elements['$nombre'].value=='') {";
                        $html[] = "alert('$value[0] es Requerido.');return FALSE;";
                        $html[] = "}";
                    }
                } else {
                    $name = $value[1]->getId();
                    $html[] = "if(document.getElementById('$id').elements['$name'].value=='') {";
                    $html[] = "alert('$value[0] es Requerido.');return FALSE;";
                    $html[] = "}";
                }
            }
        }
        $html[] = "document.getElementById('$id').submit()";
        $html[] = "}";
        $html[] = "</script>";
        $html[] = "<form id='$id' class='form' action='$action' method='$method'>";
        foreach ($this->elements as $value) {
            if ($value[0]) {
                $html[] = '' . $value[0] . '<br>';
                if (is_array($value[1])) {
                    foreach ($value[1] as $elemento) {
                        $html[] = $elemento->generate();
                    }
                    $html[] = '<br><br>';
                } else
                    $html[] = $value[1]->generate() . '<br><br>';
            } else {
                $html[] = $value[1]->generate();
            }
        }
        $labelSubmit = $this->labelSubmit;
        $event = $this->actionCancel;
        $html[] = "<p align='right'><input type='button' onclick='$event' value='Cancelar'>&nbsp;&nbsp;";
        $html[] = "<input type='button' style='font-weight:bold' onclick='validate()' value='$labelSubmit'></p>";
        $html[] = "</form>";
        $output = '';
        foreach ($html as $code) {
            $output = $output . $code;
        }
        return (String) $output;
    }

    /**
     * Agrega uno o mas campos al Formulario
     * @param String $label Etiqueta del Campo
     * @param Element|Array $element Objeto Element
     * @param Boolean $required Indica si el campo es Requerido
     */
    public function addElement($label,$element, $required = TRUE) {
        $this->elements[] = array($label, $element, $required);
    }
    
    /**
     * Obtiene los Elementos del Formulario
     * @return Element|Array
     */
    public function getElements() {
        return $this->elements;
    }

}
?>
